package com.kd.crawler.parser;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.kd.crawler.dao.AnalyzerDetailsDao;
import com.kd.crawler.dao.OriginalHtmlDao;
import com.kd.crawler.dao.StatisticEntryUrlDao;
import com.kd.crawler.entity.CrawlerEntry;
import com.kd.crawler.entity.Document;

public class ParserDetails {

	private static final Logger logger = Logger.getLogger(ParserDetails.class);
	
	public static void main(String[] args) {
		int start = 0;
		int limit = 100;
		while(true) {
			List<CrawlerEntry> ces = OriginalHtmlDao.getInstance().query(start, limit);
			if(null==ces || 0==ces.size()) {
				logger.info("select completed!");
				break;
			}
			for(CrawlerEntry ce : ces) {
				//old version
				parseForOld(ce, "O");
				//new version
				parseForNew(ce, "N");
				OriginalHtmlDao.getInstance().update(ce.getId(), "Y");
			}
		}
		
	}
	// old version
	private static HashMap<String,HtmlParser> parserCache = new HashMap<String,HtmlParser>();
	public static void parseForOld(CrawlerEntry ce, String analyzerFlag) {
		logger.info("old version parser start for url[" + ce.getUrl() + "] at id: " + ce.getId());
		//this is old
		HtmlParser parser=parserCache.get(ce.getParserName());
		if(parser==null){
			parser= new HtmlParserImpl(ce.getParserName());
			parserCache.put(ce.getParserName(),parser);
		}
		Document meta = parser.parse(ce);
		//start to statistic
		AnalyzerDetailsDao.getInstance().insert(meta, ce.getId(), analyzerFlag);
		StatisticEntryUrlDao.getInstance().insert(ce, 0, analyzerFlag);
		StatisticEntryUrlDao.getInstance().update(ce, meta, analyzerFlag);
		logger.info("old version parser has completed for url[" + ce.getUrl() + "] at id: " + ce.getId());
	}
	
	//new version
	private static final HtmlParser parser = new StandardAnalyzer();
	public static void parseForNew(CrawlerEntry ce, String analyzerFlag) {
		logger.info("new version parser start for url[" + ce.getUrl() + "] at id: " + ce.getId());
		Document meta = null;
		try {
			meta = parser.parse(ce);
		} catch (Exception e) {
			meta = new Document();
			e.printStackTrace();
		} finally {
			//start to statistic
			AnalyzerDetailsDao.getInstance().insert(meta, ce.getId(), analyzerFlag);
			StatisticEntryUrlDao.getInstance().insert(ce, 0, analyzerFlag);
			StatisticEntryUrlDao.getInstance().update(ce, meta, analyzerFlag);
			logger.info("new version parser has completed for url[" + ce.getUrl() + "] at id: " + ce.getId());
		}
	}
}
